package com.yunkont.pigcontrol.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yunkont.pigcontrol.R;
import com.yunkont.pigcontrol.bean.HistoryBean;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by bomei on 2017/9/23.
 */

public class HistroyAllAdapter extends RecyclerView.Adapter {

    private Context context;
    private HistoryBean historyBean;

    public HistroyAllAdapter(Context context, HistoryBean historyBean) {
        this.context = context;
        this.historyBean = historyBean;
    }

    public void setHistoryBean(HistoryBean historyBean) {
        this.historyBean = historyBean;
        Log.e("setHistoryBean", "setHistoryBean: "+historyBean.getCount().size()+" "+historyBean.getData().getData().size());
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_history_all, parent, false);
        return new ItemViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder holder1 = (ItemViewHolder) holder;
        try {
            HistoryBean.CountBean countBean = historyBean.getCount().get(position);
            holder1.tvTime.setText(""+countBean.getDay());
            holder1.tvSleepTang.setText("站(静止)："+countBean.getPosture_2()+"小时      趴(静止)："+countBean.getPosture_3()+"小时");
            holder1.tvSeatSport.setText("躺(静止)："+countBean.getPosture_4()+"小时      运动："+countBean.getPosture_1()+"小时");
            holder1.tvPigName.setText("猪编号："+ historyBean.getData().getData().get(0).getPig_id());
        }catch (Exception e){e.printStackTrace();}
    }

    @Override
    public int getItemCount() {
        int i = historyBean.getCount() == null ? 0 : historyBean.getCount().size();
        Log.e("adapter1", "getItemCount: "+i );
        return i;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tvTime)
        TextView tvTime;
        @Bind(R.id.tvPigName)
        TextView tvPigName;
        @Bind(R.id.tvSleepTang)
        TextView tvSleepTang;
        @Bind(R.id.tvSeatSport)
        TextView tvSeatSport;

        ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
